package com.yunfeisoft.business.controller;

import com.yunfeisoft.business.model.DonationAssignment;
import com.yunfeisoft.business.model.DonationAssignmentItem;
import com.yunfeisoft.business.service.inter.DonationAssignmentItemService;
import com.yunfeisoft.business.service.inter.DonationAssignmentService;
import com.yunfeisoft.common.base.BaseController;
import com.yunfeisoft.common.utils.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * ClassName: DonationAssignmentController
 * Description: 捐赠物品分配Controller
 * Author: Jackie liu
 * Date: 2020-05-14
 */
@Controller
public class DonationAssignmentController extends BaseController {

    @Autowired
    private DonationAssignmentService donationAssignmentService;
    @Autowired
    private DonationAssignmentItemService donationAssignmentItemService;

    /**
     * 添加捐赠物品分配
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/donationAssignment/save", method = RequestMethod.POST)
    @ResponseBody
    public Response save(DonationAssignment record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "happenDate", "日期为空");
        validator.required(request, "dept", "接受部门为空");
        validator.required(request, "type", "类型为空");
        validator.required(request, "handleName", "经办人为空");
        validator.required(request, "elderName", "老人代表为空");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }

        record.setId(KeyUtils.getKey());
        if (!CollectionUtils.isEmpty(record.getDonationAssignmentItemList())) {
            Iterator<DonationAssignmentItem> iterator = record.getDonationAssignmentItemList().iterator();
            while (iterator.hasNext()) {
                DonationAssignmentItem donationAssignmentItem = iterator.next();
                if (donationAssignmentItem.getExists() == null) {
                    iterator.remove();
                    continue;
                }
                donationAssignmentItem.setDonationAssignmentId(record.getId());
            }
        }
        donationAssignmentService.save(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 修改捐赠物品分配
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/donationAssignment/modify", method = RequestMethod.POST)
    @ResponseBody
    public Response modify(DonationAssignment record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "id", "参数错误");
        validator.required(request, "happenDate", "日期为空");
        validator.required(request, "dept", "接受部门为空");
        validator.required(request, "type", "类型为空");
        validator.required(request, "handleName", "经办人为空");
        validator.required(request, "elderName", "老人代表为空");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }

        if (!CollectionUtils.isEmpty(record.getDonationAssignmentItemList())) {
            Iterator<DonationAssignmentItem> iterator = record.getDonationAssignmentItemList().iterator();
            while (iterator.hasNext()) {
                DonationAssignmentItem donationAssignmentItem = iterator.next();
                if (donationAssignmentItem.getExists() == null) {
                    iterator.remove();
                    continue;
                }
                donationAssignmentItem.setDonationAssignmentId(record.getId());
            }
        }
        donationAssignmentService.modify(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 查询捐赠物品分配
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/donationAssignment/query", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response query(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        DonationAssignment record = donationAssignmentService.load(id);
        if (record != null) {
            List<DonationAssignmentItem> donationAssignmentItemList = donationAssignmentItemService.queryByDonationAssignmentId(id);
            record.setDonationAssignmentItemList(donationAssignmentItemList);
        }
        return ResponseUtils.success(record);
    }

    /**
     * 分页查询捐赠物品分配
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/donationAssignment/list", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response list(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = new HashMap<String, Object>();
        initParams(params, request);
        Page<DonationAssignment> page = donationAssignmentService.queryPage(params);
        return ResponseUtils.success(page);
    }

    /**
     * 批量删除捐赠物品分配
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/donationAssignment/delete", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response delete(HttpServletRequest request, HttpServletResponse response) {
        String ids = ServletRequestUtils.getStringParameter(request, "ids", null);
        if (StringUtils.isBlank(ids)) {
            return ResponseUtils.warn("参数错误");
        }
        donationAssignmentService.remove(ids.split(","));
        return ResponseUtils.success("删除成功");
    }
}
